package org.jeecg.modules.PriceSystemModule.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.PriceSystemModule.entity.PriceInervalEntity;
import org.jeecg.modules.PriceSystemModule.entity.PriceSystemEntity;
import org.jeecg.modules.PriceSystemModule.service.TechnologyCostManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/TechnologyCostManage")
public class TechnologyCostManageController {

    @Autowired
    TechnologyCostManageService service;

    /**
     * 查询工艺品类成本管理列表
     *
     * @param pageSize
     * @param pageNo
     * @param category
     * @param businessType
     * @param sysuserId
     * @return
     */
    @GetMapping("/CostManageList")
    public Result<?> getCostManageList(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "category", defaultValue = "0") Integer category,
            @RequestParam(name = "businessType", defaultValue = "10") Integer businessType,
            @RequestParam(name = "sysuserId") String sysuserId
    ) {
        Page<PriceSystemEntity> page = new Page<PriceSystemEntity>(pageNo, pageSize);
        IPage<PriceSystemEntity> pageList = service.getCostManageList(page, category, businessType, sysuserId);
        return Result.ok(pageList);
    }

    /**
     * 查询某工艺品类成本的阶梯成本数据
     *
     * @param pricesystemFactoryId
     * @return
     */
    @GetMapping("/costInervallist")
    public Result<?> costInervallist(
            @RequestParam(name = "pricesystemFactoryId") String pricesystemFactoryId
    ) {
        List<PriceInervalEntity> list = service.getcostInervallist(pricesystemFactoryId);
        return Result.ok(list);
    }


    /**
     * 假删除这条工艺类型成本阶梯价格区间数据
     */
    @GetMapping("/deletecostInerval")
    public Result<?> deletecostInerval(
            @RequestParam(name = "id") int id
    ) {
        try {
            int count = service.deletecostInerval(id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("删除失败");
        }
    }

    /**
    为这个成本价格体系id添加一条成本区间数据
    */
    @PostMapping("/addcostInerval")
    public Result<?> addcostInerval(
            @RequestBody PriceInervalEntity entity
    ) {
        try {
            if(entity.getMinNumbers()>=entity.getMaxNumbers()){
                return Result.error("最大数小于等于最小数，请重新填写售价金额");
            }
            //把列表查询出来，循环拍断是否覆盖
            List<PriceInervalEntity>  list=service.getPriceInervalEntitylist(entity.getPricesystemFactoryId());
            if(list!=null){
                for(int i=0;i<list.size();i++){
                    if( entity.getMaxNumbers()>=list.get(i).getMinNumbers()&&entity.getMaxNumbers()<list.get(i).getMaxNumbers()){
                        return Result.error("所添加的阶梯售价金额重复，请修改");
                    }
                    if( entity.getMinNumbers()>=list.get(i).getMinNumbers()&&entity.getMinNumbers()<list.get(i).getMaxNumbers()){
                        return Result.error("所添加的阶梯售价金额重复，请修改");
                    }
                }
            }

            //判断一下这个成本价格体系的这个价格区间是否已经设置
            String oldcz=service.costInervaloldcz(entity.getPricesystemFactoryId(),entity.getMinNumbers(),entity.getMaxNumbers());
            if(oldcz!=null){
                return Result.error("该售价金额区间已经设置,请更改售价金额区间");
            }
            int count = service.addcostInerval(entity);
            if(count>0)
                service.updatestatus(entity.getPricesystemFactoryId());
            return Result.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ok("请输入正确的售价金额");
        }
    }


     /*
     通过父级id查询下面所有的工艺
      */
    @GetMapping("/getallsongongyi")
    public Result<?> getallsongongyi(
            @RequestParam(name = "parentId") String parentId
    ){
            List<Map<String,String>> list=service.getallsongongyi(parentId);
            return Result.ok(list);
    }

    /*
    新增成本体系
     */
    @PostMapping("/addcostSystem")
    public Result<?> addcostSystem(
            @RequestBody HashMap<String,String> map
            ){
        //查询这个供应商的这个工艺是否已经设置过
        int cunzai=service.getsysgongyicost(map.get("gongyi"),map.get("sysuserId"));
        if(cunzai>0)
            return Result.error("该工艺类型已经设置过，请选择其他工艺");
        int count=service.addcostSystem(map.get("gongyi"),map.get("category"),map.get("sysuserId"));
        if(count>0){
            return Result.ok("新增成功");
        }else {
            return Result.error("新增失败");
        }
    }
}
